﻿using System;
using System.Data;
using System.Windows.Forms;


namespace CarBlackBox
{
    [Serializable]
    public class DriverInfoData
    {

        public const string ADDRESS_Field = @"Address";

        public const string CARTYPE_Field = @"CarType";

        public const string DIN_Field = @"DIN";

        public const string PRIMARYKEY_Field = @"DIN";

        public const string DRIVEAGE_Field = @"DriveAge";

        public const string DRIVERID_Field = @"DriverID";

        public const string HASSET_Field = @"HasSet";

        public const string HOLDDATE_Field = @"HoldDate";

        public const string LASTUPDATETIME_Field = @"LastUpdateTime";

        public const string LICENSEPLATE_Field = @"LicensePlate";

        public const string MEMO_Field = @"Memo";

        public const string NAME_Field = @"Name";

        public const string OPERATOR_Field = @"Operator";

        public const string PHONE_Field = @"Phone";

        public const string SEX_Field = @"Sex";

        public const string STARTWORKDATE_Field = @"StartWorkDate";

        public const string VALIDDATE_Field = @"ValidDate";

        private String _Address;     //单位或住址

        private String _CarType;     //准驾车型

        private String _DIN;     //驾驶证号

        private Int16 _DriveAge;     //驾龄

        private Int32 _DriverID;     //驾驶员代码，由使用者定义

        private Boolean _HasSet;     //是否已设置身份卡

        private String _HoldDate;     //领证日期

        private DateTime _LastUpdateTime;     //最后更新时间

        private String _LicensePlate;     //主驾车辆牌照

        private String _Memo;     //个人简历

        private String _Name;     //姓名

        private String _Operator;     //操作员

        private String _Phone;     //联系电话

        private String _Sex;     //性别

        private String _StartWorkDate;     //参加工作时间

        private Int16 _ValidDate;     //证件有效期

        public String Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        public String CarType
        {
            get { return _CarType; }
            set { _CarType = value; }
        }
        public String DIN
        {
            get { return _DIN; }
            set { _DIN = value; }
        }
        public Int16 DriveAge
        {
            get { return _DriveAge; }
            set { _DriveAge = value; }
        }
        public Int32 DriverID
        {
            get { return _DriverID; }
            set { _DriverID = value; }
        }
        public Boolean HasSet
        {
            get { return _HasSet; }
            set { _HasSet = value; }
        }
        public String HoldDate
        {
            get { return _HoldDate; }
            set { _HoldDate = value; }
        }
        public DateTime LastUpdateTime
        {
            get { return _LastUpdateTime; }
            set { _LastUpdateTime = value; }
        }
        public String LicensePlate
        {
            get { return _LicensePlate; }
            set { _LicensePlate = value; }
        }
        public String Memo
        {
            get { return _Memo; }
            set { _Memo = value; }
        }
        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public String Operator
        {
            get { return _Operator; }
            set { _Operator = value; }
        }
        public String Phone
        {
            get { return _Phone; }
            set { _Phone = value; }
        }
        public String Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }
        public String StartWorkDate
        {
            get { return _StartWorkDate; }
            set { _StartWorkDate = value; }
        }
        public Int16 ValidDate
        {
            get { return _ValidDate; }
            set { _ValidDate = value; }
        }
        public DriverInfoData()
        {
            _Address = "";
            _CarType = "";
            _DIN = "";
            _DriveAge = 0;
            _DriverID = 0;
            _HasSet = false;
            _HoldDate = "";
            _LastUpdateTime = DateTime.Now;
            _LicensePlate = "";
            _Memo = "";
            _Name = "";
            _Operator = "当前操作员";
            _Phone = "";
            _Sex = "";
            _StartWorkDate = "";
            _ValidDate = 0;
        }
    }

    public class DriverInfoDataFactory
    {
        public static DriverInfoData Construct(IDataReader reader)
        {
            DriverInfoData data = new DriverInfoData();

            data.Address = Convert.ToString(reader["Address"]);

            data.CarType = Convert.ToString(reader["CarType"]);

            data.DIN = Convert.ToString(reader["DIN"]);

            data.DriveAge = Convert.ToInt16(reader["DriveAge"]);

            data.DriverID = Convert.ToInt32(reader["DriverID"]);

            data.HasSet = Convert.ToBoolean(reader["HasSet"]);

            data.HoldDate = Convert.ToString(reader["HoldDate"]);

            data.LastUpdateTime = Convert.ToDateTime(reader["LastUpdateTime"]);

            data.LicensePlate = Convert.ToString(reader["LicensePlate"]);

            data.Memo = Convert.ToString(reader["Memo"]);

            data.Name = Convert.ToString(reader["Name"]);

            data.Operator = Convert.ToString(reader["Operator"]);

            data.Phone = Convert.ToString(reader["Phone"]);

            data.Sex = Convert.ToString(reader["Sex"]);

            data.StartWorkDate = Convert.ToString(reader["StartWorkDate"]);

            data.ValidDate = Convert.ToInt16(reader["ValidDate"]);

            return data;
        }
        public static String GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static String GetPrimaryKey(DataTable dt, int rowIndex)
        {
            String PrimaryKey = null;

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { PrimaryKey = Convert.ToString(dr[DriverInfoData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static DriverInfoData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static DriverInfoData Construct(DataTable dt, int rowIndex)
        {
            DriverInfoData data = new DriverInfoData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.Address = Convert.ToString(dr["Address"]); }
            catch { }

            try { data.CarType = Convert.ToString(dr["CarType"]); }
            catch { }

            try { data.DIN = Convert.ToString(dr["DIN"]); }
            catch { }

            try { data.DriveAge = Convert.ToInt16(dr["DriveAge"]); }
            catch { }

            try { data.DriverID = Convert.ToInt32(dr["DriverID"]); }
            catch { }

            try { data.HasSet = Convert.ToBoolean(dr["HasSet"]); }
            catch { }

            try { data.HoldDate = Convert.ToString(dr["HoldDate"]); }
            catch { }

            try { data.LastUpdateTime = Convert.ToDateTime(dr["LastUpdateTime"]); }
            catch { }

            try { data.LicensePlate = Convert.ToString(dr["LicensePlate"]); }
            catch { }

            try { data.Memo = Convert.ToString(dr["Memo"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.Operator = Convert.ToString(dr["Operator"]); }
            catch { }

            try { data.Phone = Convert.ToString(dr["Phone"]); }
            catch { }

            try { data.Sex = Convert.ToString(dr["Sex"]); }
            catch { }

            try { data.StartWorkDate = Convert.ToString(dr["StartWorkDate"]); }
            catch { }

            try { data.ValidDate = Convert.ToInt16(dr["ValidDate"]); }
            catch { }

            return data;
        }
        public static DriverInfoData Construct(DataGridView dgv, int rowIndex)
        {
            DriverInfoData data = new DriverInfoData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                switch (dgv.Columns[i].DataPropertyName)
                {
                    case "Address":
                        try { data.Address = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "CarType":
                        try { data.CarType = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "DIN":
                        try { data.DIN = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "DriveAge":
                        try { data.DriveAge = Convert.ToInt16(dr[i].Value); }
                        catch { }
                        break;
                    case "DriverID":
                        try { data.DriverID = Convert.ToInt32(dr[i].Value); }
                        catch { }
                        break;
                    case "HasSet":
                        try { data.HasSet = Convert.ToBoolean(dr[i].Value); }
                        catch { }
                        break;
                    case "HoldDate":
                        try { data.HoldDate = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "LastUpdateTime":
                        try { data.LastUpdateTime = Convert.ToDateTime(dr[i].Value); }
                        catch { }
                        break;
                    case "LicensePlate":
                        try { data.LicensePlate = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Memo":
                        try { data.Memo = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Name":
                        try { data.Name = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Operator":
                        try { data.Operator = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Phone":
                        try { data.Phone = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Sex":
                        try { data.Sex = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "StartWorkDate":
                        try { data.StartWorkDate = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "ValidDate":
                        try { data.ValidDate = Convert.ToInt16(dr[i].Value); }
                        catch { }
                        break;
                }
            }
            return data;
        }
        public static String GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            String PrimaryKey = null;

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == DriverInfoData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToString(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
    }
}